import axios from 'axios'
import {Message} from 'element-ui'
import VueCookie from "vue-cookie";
// import env from '../env.js'

const request = axios.create({
    baseURL: '/api', //根据前端的跨域方式做调整   /a/b ： /api/a/b => /a/b
    timeout: 30000  //请求最多30秒，超过即失败
})

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(response => {
    let res = response.data
    let path = location.pathname

    // console.log(path);
    if (res.status == 0) {
        return res.data
    } else if (res.status == 10) {
        VueCookie.delete('satoken')
        if (path != '/login' && path != '/index') {
            window.location.href = '/login'
        }
        Message.error(res.msg)
        return Promise.reject(res.msg)
    } else {
        // Message.warning(res.msg)
        return Promise.reject(res.msg)
    }
}, (error) => {
    let res = error.response
    Message.error(res.data.message)
    return Promise.reject(error)
})

export default request